package com.rabbitmq.controller;

import com.rabbitmq.dto.OrderTypeFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "demo")
public class DemoController {


    @Autowired
    private AmqpTemplate rabbitTemplate;


    /**
     * 下单
     */
    @GetMapping(value = "order")
    public String order() {
        OrderTypeFactory.getInstance().setType(0);
        rabbitTemplate.convertAndSend("ex.do", "key.do", "请在10秒内支付");
        return "商品已经下单，请在10秒内支付";
    }

    /**
     * 支付
     *
     * @return
     */
    @GetMapping(value = "pay")
    public String pay() {
        if (OrderTypeFactory.getInstance().getType() == 0) {
            String msg = (String) rabbitTemplate.receiveAndConvert("q.do");
            System.out.println("支付为支付的订单:" + msg);
            OrderTypeFactory.getInstance().setType(1);
            return "订单支付成功";
        } else if (OrderTypeFactory.getInstance().getType() == 1) {
            return "订单已支付，请勿重新点击";
        } else if (OrderTypeFactory.getInstance().getType() == 2) {
//            String msg = (String) rabbitTemplate.receiveAndConvert("q.do.dlx");
//            System.out.println("支付为支付的订单:" + msg);
            return "订单已取消";
        }
        return null;
    }


}
